package com.eleaf.adbb.util;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class BaseUtil {

	private static StringBuilder sb;

	/**
	 * List去重
	 * 
	 * @param list
	 * @return
	 */
	public static <T> List<T> removeDuplicateWithOrder( List<T> list ) {
		if ( list == null || list.size () == 0 )
			return list;
		Set<T> set = new HashSet<T> ();
		List<T> newList = new ArrayList<T> ();
		for ( Iterator<T> iter = list.iterator (); iter.hasNext (); ) {
			T element = iter.next ();
			if ( set.add ( element ) )
				newList.add ( element );
		}
		return newList;
	}

	/**
	 * 字符串连接
	 * 
	 * @param strings
	 * @return
	 */
	public static String appendString( String... strings ) {
		if ( sb == null )
			sb = new StringBuilder ();
		sb.delete ( 0, sb.length () );
		for ( String str : strings ) {
			sb.append ( str );
		}
		return sb.toString ();
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat ( "yyyyMMddHHmmss" );
		Date curDate = new Date ( System.currentTimeMillis () );// 获取当前时间
		return formatter.format ( curDate );
	}

	public static String formatToString( String date, String format ) {
		try {
			Date dt = parseStringToDate ( date );
			java.text.SimpleDateFormat sdf = new SimpleDateFormat ( format );
			return sdf.format ( dt );
		} catch ( Exception e ) {
			e.printStackTrace ();
		}
		return date;
	}

	public static Date parseStringToDate( String date ) throws Exception {
		Date result = null;
		String parse = date;
		parse = parse.replaceFirst ( "^[0-9]{4}([^0-9]?)", "yyyy$1" );
		parse = parse.replaceFirst ( "^[0-9]{2}([^0-9]?)", "yy$1" );
		parse = parse.replaceFirst ( "([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2" );
		parse = parse.replaceFirst ( "([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2" );
		parse = parse.replaceFirst ( "( )[0-9]{1,2}([^0-9]?)", "$1HH$2" );
		parse = parse.replaceFirst ( "([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2" );
		parse = parse.replaceFirst ( "([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2" );

		SimpleDateFormat format = new SimpleDateFormat ( parse );

		result = format.parse ( date );

		return result;
	}

	public static String MD5( String s ) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] strTemp = s.getBytes ();
			MessageDigest mdTemp = MessageDigest.getInstance ( "MD5" );
			mdTemp.update ( strTemp );
			byte[] md = mdTemp.digest ();

			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for ( int i = 0; i < j; i++ ) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String ( str );
		} catch ( Exception e ) {
			return null;
		}
	}

}
